import store from "../../store/index";
import create from "../../utils1/create"
// Componet/Componet.js
create.Component(store, {
  /**
   * 组件的属性列表
   */
  properties: {
    propArray: {
      type: Array,
    },
    propNewid: {
      type: String,
      observer: function (newVal, oldVal) {
        console.log("oldVal", oldVal)
        console.log("newVal", newVal)
        this.updateSelectedOption(newVal);
      }
    },
    selectdate: {
      type: Date,
      observer: function (newVal, oldVal) {
        console.log("oldVal", oldVal)
        console.log("newVal", newVal)
        this.updateSelectedOption(newVal);
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    selectShow: false, //初始option不显示
    nowText: "请选择", //初始内容
    animationData: {} //右边箭头的动画
  },
  /**
   * 组件的方法列表
   */
  methods: {
    updateSelectedOption: function (newid) {
      console.log("id", newid)
      // 根据 id 更新选中值
      //  this.setText1(newid)
    },
    //option的显示与否
    selectToggle: function () {
      var nowShow = this.data.selectShow; //获取当前option显示的状态
      //创建动画
      var animation = wx.createAnimation({
        timingFunction: "ease"
      })
      this.animation = animation;
      if (nowShow) {
        animation.rotate(0).step();
        this.setData({
          animationData: animation.export()
        })
      } else {
        animation.rotate(180).step();
        this.setData({
          animationData: animation.export()
        })
      }
      this.setData({
        selectShow: !nowShow
      })
    },
    selectToggle1: function () {
      var nowShow = this.data.selectShow; //获取当前option显示的状态
      //创建动画
      var animation = wx.createAnimation({
        timingFunction: "ease"
      })
      this.animation = animation;
      if (nowShow) {
        animation.rotate(0).step();
        this.setData({
          animationData: animation.export()
        })
      } else {
        animation.rotate(180).step();
        this.setData({
          animationData: animation.export()
        })
      }
      this.setData({
        selectShow: !nowShow
      })
    },
    formatDate: function (date) {
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, '0');
      const day = String(date.getDate()).padStart(2, '0');
      return `${year}`;
    },

    // const currentDate = new Date();
    // const oneYearAgo = new Date(currentDate.getTime() - (365 * 24 * 60 * 60 * 1000));
    // oneYearAgo 是距离当前日期一年前的日期对象

    //设置内容
    setText: function (e) {
      console.log("eee", e)
      console.log("eeee", this.newid)
      console.log("this.properties", this.properties)
      var nowData = this.properties.propArray; //当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
      var nowIdx = e.target.dataset.index; //当前点击的索引
      var nowText = nowData[nowIdx].text; //当前点击的内容
      if (nowIdx == 0) {
        var End_Year = this.formatDate(new Date)
        var hidden = false
        var Begin_Year = this.formatDate(new Date(new Date().getTime() - (365 * 24 * 60 * 60 * 1000)))
        store.data.zdy.date1 = ""
        store.data.zdy.date2 = ''
      } else if (nowIdx == 1) {
        var End_Year = this.formatDate(new Date)
        var hidden = false
        var Begin_Year = this.formatDate(new Date(new Date().getTime() - (3 * 365 * 24 * 60 * 60 * 1000)))
        store.data.zdy.date1 = ""
        store.data.zdy.date2 = ''
      } else if (nowIdx == 2) {
        var End_Year = this.formatDate(new Date)
        var hidden = false
        var Begin_Year = this.formatDate(new Date(new Date().getTime() - (5 * 365 * 24 * 60 * 60 * 1000)))
        store.data.zdy.date1 = ""
        store.data.zdy.date2 = ''
      } else if (nowIdx == 3) {
        // var End_Year=""
        // var Begin_Year=""
        var hidden = true
      }
      // var End_Year= this.formatDate(new Date(new Date().getTime() - (365 * 24 * 60 * 60 * 1000)))
      console.log("Begin_Year", Begin_Year)
      console.log("End_Year", End_Year)
      //再次执行动画，注意这里一定，一定，一定是this.animation来使用动画
      this.animation.rotate(0).step();
      this.setData({
        selectShow: false,
        nowText: nowText,
        animationData: this.animation.export()
      })
      this.triggerEvent('myevent', {
        name: nowText,
        id: nowIdx,
        Begin_Year: Begin_Year,
        End_Year: End_Year,
        hidden: hidden
      })
    },
    setText1: function (e) {
      console.log("eee", e)
      console.log("eeee", this.newid)
      console.log(this.properties)
      var nowData = this.properties.propArray; //当前option的数据是引入组件的页面传过来的，所以这里获取数据只有通过this.properties
      var nowIdx = e; //当前点击的索引
      var nowText = nowData[nowIdx].text; //当前点击的内容
      //再次执行动画，注意这里一定，一定，一定是this.animation来使用动画
      this.selectToggle1()
      this.animation.rotate(0).step();
      this.setData({
        selectShow: false,
        nowText: nowText,
        animationData: this.animation.export()
      })
      this.triggerEvent('myevent', {
        name: nowText,
        id: nowIdx
      })
    }
  }
})